package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.WarehouseMapper;
import com.zhijian.warehouse.manage.entity.dto.UserDto;
import com.zhijian.warehouse.manage.entity.po.Warehouse;
import com.zhijian.warehouse.manage.entity.vo.request.WarehouseSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.WarehouseSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreWarehouseVo;
import com.zhijian.warehouse.manage.entity.vo.response.WarehouseVo;
import com.zhijian.warehouse.manage.enums.DelFlagEnum;
import com.zhijian.warehouse.manage.enums.StatusEnum;
import com.zhijian.warehouse.manage.holder.CurrentWebUserHolder;
import com.zhijian.warehouse.manage.service.WarehouseService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 仓库业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-10 19:10:55
 */
@Slf4j
@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseMapper mapper;

    @Override
    public Warehouse selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public WarehouseVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        Warehouse warehouse = this.selectById(id);
        AssertCommonUtil.notNull(warehouse, "此数据不存在或者已被删除");
        AssertCommonUtil.check(Objects.equals(Integer.valueOf(warehouse.getStatus()), StatusEnum.NORMAL.getCode()), "启用状态无法删除");
        this.updateByPrimaryKeySelective(Warehouse.builder().id(id).isDelete(DelFlagEnum.DELETED.value).build());
        // mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            for (Long l : idList) {
                this.deleteById(l);
            }
        }
    }

    @Override
    public Long insert(Warehouse warehouse) {
        Date now = new Date();
        warehouse.setCreateTime(now);
        warehouse.setUpdateTime(now);
        return mapper.insert(warehouse) > 0 ? warehouse.getId() : 0L;
    }

    @Override
    public Long insertSelective(Warehouse warehouse) {
        Date now = new Date();
        warehouse.setCreateTime(now);
        warehouse.setUpdateTime(now);
        return mapper.insertSelective(warehouse) > 0 ? warehouse.getId() : 0L;
    }

    @Override
    public boolean update(Warehouse warehouse) {
        Date now = new Date();
        warehouse.setUpdateTime(now);
        return mapper.updateByPrimaryKey(warehouse) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Warehouse record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(WarehouseSaveOrUpdateVo saveOrUpdateVo) {
        Integer count = this.count(WarehouseSearchVo.builder()
                .number(saveOrUpdateVo.getNumber())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "编号重复");
        count = this.count(WarehouseSearchVo.builder()
                .nameAccurate(saveOrUpdateVo.getName())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "名称重复");
        Warehouse warehouse = new Warehouse();
        BeanUtils.copyProperties(saveOrUpdateVo, warehouse);
        boolean b = this.insertSelective(warehouse) != 0;
        saveOrUpdateVo.setId(warehouse.getId());
        return b;
    }

    @Override
    public boolean modify(WarehouseSaveOrUpdateVo saveOrUpdateVo) {
        Warehouse old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        Integer count = this.count(WarehouseSearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .number(saveOrUpdateVo.getNumber())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "编号重复");
        count = this.count(WarehouseSearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .nameAccurate(saveOrUpdateVo.getName())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "名称重复");
        old.setNumber(saveOrUpdateVo.getNumber());
        old.setName(saveOrUpdateVo.getName());
        old.setAddress(saveOrUpdateVo.getAddress());
        old.setSquareMeasure(saveOrUpdateVo.getSquareMeasure());
        old.setHeader(saveOrUpdateVo.getHeader());
        old.setHeaderPhone(saveOrUpdateVo.getHeaderPhone());
        old.setStatus(saveOrUpdateVo.getStatus());
        old.setUpdateUserId(saveOrUpdateVo.getUpdateUserId());
        old.setUpdateUserName(saveOrUpdateVo.getUpdateUserName());
        old.setIsDelete(DelFlagEnum.USING.value);
        return this.update(old);
    }

    @Override
    public WarehouseVo selectOne(WarehouseSearchVo warehouseSearchVo) {
        return mapper.selectOne(warehouseSearchVo);
    }

    @Override
    public Integer count(WarehouseSearchVo warehouseSearchVo) {
        return mapper.count(warehouseSearchVo);
    }

    @Override
    public List<WarehouseVo> selectList(WarehouseSearchVo warehouseSearchVo) {
        return mapper.selectList(warehouseSearchVo);
    }

    @Override
    public Page<WarehouseVo> selectPage(WarehouseSearchVo warehouseSearchVo) {
        AssertCommonUtil.check(Objects.isNull(warehouseSearchVo.getPageNum()) || Objects.isNull(warehouseSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(warehouseSearchVo);
        //分页
        Page<WarehouseVo> page = new Page<>(true, count, warehouseSearchVo.getPageNum(), warehouseSearchVo.getPageSize());
        warehouseSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<WarehouseVo> responseVos = this.selectList(warehouseSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<Warehouse> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void changeStatus(Long id, Byte status) {
        Warehouse old = this.selectById(id);
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        old.setStatus(status);
        UserDto user = CurrentWebUserHolder.getUser();
        old.setUpdateUserId(user.getId());
        old.setUpdateUserName(user.getName());
        this.update(old);
    }

    @Override
    public List<StoreWarehouseVo> selectByIds(List<Long> warehouseIdsList) {
        return mapper.selectByIds(warehouseIdsList);
    }
}
